using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ClientTools.Client.Controls.Versions
{
    public partial class ucVersions : Client.Controls.BaseUserControl, Client.Controls.IUserControl
    {

        #region Private Variables
        private Library.Interfaces.IVersions _ActVersions;
        #endregion

        public ucVersions()
        {
            InitializeComponent();
        }

        public void ClearData()
        {
            this.Label1.Text = "-";
            this.lblMessage1.Text = "";
            this.lblMessage2.Text = "";
            this.pnlContent.Enabled = false;
        }

        private void GetVersions()
        {
            this.ClearData();

            global::ClientTools.Library.Versions.VersionInfo vi;
            try
            {
                vi = this._ActVersions.GetVersionInfos();
            }
            catch (Exception ex)
            {
                this.ClearData();
                this.Enabled = false;
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.Label1.Text = "There are " + vi.Content.ToString() + " content items having " + vi.Versions.ToString() + " versions, " + vi.Documents.ToString() + " documents and " + vi.PropertyData.ToString() + " property data in the database...";
            this.pnlContent.Enabled = true;
        }

        private void tsbGetVersions_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.GetVersions();
            this.Cursor = Cursors.Default;
        }

        private void cmdClearVersionsByDate_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            global::ClientTools.Library.Versions.VersionInfo vi;
            try
            {
                vi = this._ActVersions.DeleteVersionsByDate(this.DateTimePicker1.Value);
            }
            catch (Exception ex)
            {
                this.ClearData();
                this.Enabled = false;
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.lblMessage1.Text = "Cleared " + vi.Versions.ToString() + " versions with " + vi.PropertyData.ToString() + " property data and " + vi.Documents.ToString() + " documents of " + vi.Content.ToString() + " content items!";
            this.GetVersions();

            this.Cursor = Cursors.Default;
        }

        private void cmdClearVersionsByNumberOfItems_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            global::ClientTools.Library.Versions.VersionInfo vi;
            try
            {
                vi = this._ActVersions.DeleteVersionsByNumberOfItems(Decimal.ToInt32(this.NumericUpDown1.Value));
            }
            catch (Exception ex)
            {
                this.ClearData();
                this.Enabled = false;
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.lblMessage2.Text = "Cleared " + vi.Versions.ToString() + " versions with " + vi.PropertyData.ToString() + " property data and " + vi.Documents.ToString() + " documents of " + vi.Content.ToString() + " content items!";
            this.GetVersions();

            this.Cursor = Cursors.Default;
        }


        #region I_UserControl Member

        public string Header
        {
            get { return "Versions"; }
        }

        public void SetData(ClientTools.Client.Library.Settings.Profile profile)
        {
            this.ActProfile = profile;
            this.ActProfile.InitProvider();
            this.ActProvider = this.ActProfile.ProviderObject;

            if (!this.ImplementsVersions())
            {
                this.Enabled = false;
                return;
            }

            this._ActVersions = (Library.Interfaces.IVersions)this.ActProvider;
            this.ClearData();
            this.Enabled = true;
        }

        public Image ButtonImage
        {
            get { return this.tsbVersions.Image; }
        }

        public int Sort
        {
            get { return 5; }
        }

        public bool ShowDelimiter
        {
            get { return false; }
        }

        public string ButtonText
        {
            get { return "Versions"; }
        }

        #endregion

        private void tsbGetRootNodes_Click(object sender, EventArgs e)
        {

        }
    }
}
